package com.wdz.controller;

import com.wdz.entity.ApiUser;
import com.wdz.http.Response;
import com.wdz.fegin.FeginAdminService;
import com.wdz.service.UserService;
import com.wdz.task.UserTaskService;
import com.wdz.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;

@RestController
public class LoginController {

    //@Autowired
    //private LoginService loginService;

    @Autowired
    private TokenUtils tokenUtils;

    @Autowired
    private UserService userService;

    @Value("${token.expiration}")
    private Long expiration;

    @PostMapping("/login")
    public Response login(@RequestParam String username, @RequestParam String password,@RequestParam String ip){
        Response response = getToken(username,password,"登录成功",ip);
        if(response.getStatus() != 0){
            return response;
        }
        //添加定时任务
        Timer timer = new Timer();
        UserTaskService userTaskService = new UserTaskService();
        Map<String,Object> map = (Map<String, Object>) response.getData();
        userTaskService.setUserId((String) map.get("token"));
        timer.schedule(userTaskService,expiration*1000);
        return response;
    }

    @PostMapping("/getToken")
    public Response getToken(@RequestParam String username, @RequestParam String password){
        return getToken(username, password,"获取token成功",null);
    }
    public Response getToken(String username, String password,String message,String ip){
        Response response = feginAdminService.checkUser(username,password);
        if(response.getStatus() != 0){
            return response;
        }
        if(ip != null) {
            if (!userService.checkIsLogin(username, ip)) {
                return response.error("该用户已在其他地方登录！");
            }
        }
        ApiUser apiUser=new ApiUser();
        apiUser.setUsername(username);
        String token=tokenUtils.generateToken(apiUser);
        Map<String,Object> data = new HashMap<>();
        data.put("token",token);
        return Response.success(message,data);
    }

    private Response checkApiUser(ApiUser apiUser,String password){
        if (apiUser==null){
            return new Response(434,0,"账户不存在",null);
        }else {
            if (apiUser.getEnable()==0){
                return new Response(434,1,"账户在黑名单里",null);
            }
            if (!apiUser.getPassword().equals(password)){
                //equals相等返回true
                return new Response(434,0,"账号或密码错误",null);

            }

        }
        return null;
    }
    @Autowired
    private FeginAdminService feginAdminService;
    /**
     * 获取用户的详细信息
     */
    @PostMapping("/getUserInfo")
    public Response getUserInfo(@RequestParam String token){
        String username = this.tokenUtils.getUsernameFromToken(token);
        return feginAdminService.getUserInfo(username);
    }
}
